package com.blb.controller;

import com.blb.dto.*;
import com.blb.service.*;
import com.blb.utils.LayUIDataTable;
import com.blb.utils.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/gift")
@CrossOrigin
public class GiftController {

    @Autowired
    private ZGiftService giftService;

    @Autowired
    private ZGetgiftService zGetgiftService;

    @Autowired
    private ZUserService zUserservice;

    @Autowired
    private ZZhuboService zZhuboService;

    @Autowired
    private ZDanmuService zDanmuService;

    @Autowired
    private ZGiftService zGiftService;

    @RequestMapping("/giftall")
    @ResponseBody
    public LayUIDataTable querygift(Model model)      //查询所有礼物
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZGiftExample zGiftExample = new ZGiftExample();
        ZGiftExample.Criteria criteria = zGiftExample.createCriteria();
        List<ZGift> zGifts = giftService.selectByExample(zGiftExample);
        dt.setData(zGifts);
        dt.setCode(Status.SUCCESS);
        dt.setCount(zGifts.size());
        dt.setMsg("查询成功！");
        return dt;
    }

    @PostMapping("/songliwu/{uid}/{zid}/{cost}/{ye}/{gid}")
    @ResponseBody
    public LayUIDataTable songliwu(Model model, @PathVariable("uid") int uid, @PathVariable("zid") int zid, @PathVariable("cost")BigDecimal cost, @PathVariable("ye") BigDecimal ye, @PathVariable("gid") int gid)      //查询所有礼物
    {
        LayUIDataTable dt = new LayUIDataTable();
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String format = df.format(date).trim();
        ZGetgift zGetgift = new ZGetgift();
        zGetgift.setDate(format);
        zGetgift.setValue(cost);
        zGetgift.setUid(uid);
        zGetgift.setGid(gid);
        zGetgift.setZid(zid);
        int insert = zGetgiftService.insert(zGetgift);
        ZUser zUser = zUserservice.selectByPrimaryKey(uid);

        ZZhubo zhubo = zZhuboService.selectByPrimaryKey(zid);
        List<Integer> integers = zGetgiftService.selectBytop3(zid);
        zhubo.setWeekstart(integers+"");
        zZhuboService.updateByPrimaryKey(zhubo);            //更新该主播的贡献榜

        zUser.setMoneybag(ye.subtract(cost));
        int i = zUserservice.updateByPrimaryKey(zUser);     //扣除该用户产生的费用

        ZDanmu zDanmu = new ZDanmu();
        zDanmu.setMessage(zUser.getNichen()+"赠送了"+"<img style=\"width: 40px;height: 40px;\" src=\""+zGiftService.selectByPrimaryKey(gid).getGpicture()+"\" >");
        zDanmu.setMsgdate(format);
        zDanmu.setUid(uid);
        zDanmu.setZid(zid);
        zDanmuService.insert(zDanmu);

        dt.setData(zUser.getMoneybag());
        dt.setCode(Status.SUCCESS);
        dt.setCount(0);
        dt.setMsg("赠送成功！");
        return dt;
    }
}
